package yxy.game.pm2.active.command.weeklycard;

import com.alibaba.fastjson.JSONObject;
import yxy.apple.logger.Logger;
import yxy.apple.util.ArraryMesh;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actweekcark;
import yxy.cherry.data.bean.actweekcarkshop;
import yxy.cherry.data.bean.config;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_周卡;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.weeklycard.WeeklyCard;
import yxy.game.pm2.active.module.subordinateclass.weeklycard.WeeklyShop;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.player.FunctionHandle;

import java.util.Collection;

/**
 * 征战日志信息
 *
 * @author Dell
 */
@MessageType(proto = Command.ActWeeklyCardPayInfo, classtype = ActiveMessage.ActWeeklyCardPayInfoRequest.class)
public class CMD_Active_周卡_支付_Info extends Command<ActiveMessage.ActWeeklyCardPayInfoRequest> {

    private static final Logger logger = Logger.create(CMD_Active_周卡_支付_Info.class);

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActWeeklyCardPayInfoRequest request) throws Exception {
        String playercode = messageSource.getTag();
        ActiveMessage.ActWeeklyCardPayInfoResponse.Builder response = ActiveMessage.ActWeeklyCardPayInfoResponse.newBuilder();

        ActiveModule_周卡 am = ActiveModule_周卡.get(playercode, false);
        if (am == null) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }

        if (!am.running()) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }

        //周卡
        for (WeeklyCard wc : am.getWp().getWcs()) {
            ActiveMessage.WeeklyCard.Builder build = ActiveMessage.WeeklyCard.newBuilder();
            build.setId(wc.getId());
            build.setGoodsid(wc.getGoodsid());

            actweekcark data = ActDataCenter.getData(wc.getId(), actweekcark.class, playercode);
            if (data == null) {
                continue;
            }

            Collection<Currency> curs = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
            Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
            build.addAllCurs(items);

            int canget = DrawStatus.cannotget.getStatus();
            if (wc.getBuy() != null) {
                canget = DrawStatus.hadget.getStatus();
            } else {
                if (wc.getLock() != null) {
                    canget = DrawStatus.cannotget.getStatus();
                } else {
                    canget = DrawStatus.canget.getStatus();
                }
            }
            build.setCanget(canget);

            Currency emailcur = Currency.create(data.getSignrewardtype(), data.getSignrewardid(), data.getSignrewardnum());
            PackageMessage.Item emailitem = new CurrencyHandle().packItem(emailcur);
            build.addEmailcur(emailitem);

            response.addWcs(build.build());
        }

        //todo 相隔了几个月(动态增加商品数量 2022.08.26)
        Integer passMonth = FunctionHandle.getAlreadlyPassMonth(playercode);
        config data = DataCenter.getData(4, config.class);
        JSONObject jsonObject = JSONObject.parseObject(data.getValue());
        Integer maxraisetimes = jsonObject.getInteger("maxraisetimes");
        PlayerBean playerBean = PlayerBean.get(playercode);
        Integer begin_server = jsonObject.getInteger("begin_server");


        //商城
        for (WeeklyShop ws : am.getWp().getWss()) {
            ActiveMessage.WeeklyCardShop.Builder build = ActiveMessage.WeeklyCardShop.newBuilder();

            build.setId(ws.getId());
            build.setGoodsid(ws.getGoodsid());
            build.setLimitnum(ws.getLimitnum());
            build.setLocknum(ws.getLock().size());
            build.setBuynum(ws.getBuy().size());

            actweekcarkshop awcs = ActDataCenter.getData(ws.getId(), actweekcarkshop.class, playercode);

            int[] num = playerBean.getServerId() >= begin_server ? ArraryMesh.getNum(awcs.getShopnum(), awcs.getRaise(), passMonth, maxraisetimes) : awcs.getShopnum();

            Collection<Currency> curs = Currency.create(awcs.getShoptype(), awcs.getShopid(), num);
            Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);

            build.addAllCurs(items);

            int canget = DrawStatus.cannotget.getStatus();
            if (ws.getBuy().size() >= ws.getLimitnum()) {
                canget = DrawStatus.hadget.getStatus();
            } else {
                if (ws.getBuy().size() + ws.getLock().size() >= ws.getLimitnum()) {
                    canget = DrawStatus.cannotget.getStatus();
                } else {
                    canget = DrawStatus.canget.getStatus();
                }
            }
            build.setCanget(canget);

            response.addWss(build.build());
        }

        response.setEnd(am.getTimeline().getEndTime().getMillis());
        send(messageSource, response, Result.Success);
    }

    private void send(MessageSource messageSource, ActiveMessage.ActWeeklyCardPayInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
